package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.nhc.model.CourseInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.BottomMenuWindow;

public class CourseActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = "CourseActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, CourseActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    CourseInfo mCourseInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_activity);

        mCourseInfo = CourseInfo.getInstance();
        mCourseInfo.load(this);
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    TextView tv_showtxt;
    TextView tv_bxjz;

    TextView tv_vel_c;
    TextView tv_ctime_c;
    TextView bt_vel_left;
    TextView bt_vel_right;
    TextView bt_ctime_left;
    TextView bt_ctime_right;

    @Override
    public void initView() {//必须调用
        tv_showtxt = (TextView) findViewById(R.id.tv_showtxt);
        tv_bxjz = (TextView) findViewById(R.id.tv_bxjz);

        tv_vel_c = (TextView) findViewById(R.id.tv_vel_c);
        tv_ctime_c = (TextView) findViewById(R.id.tv_ctime_c);

        bt_vel_left = (TextView) findViewById(R.id.bt_vel_left);
        bt_vel_right = (TextView) findViewById(R.id.bt_vel_right);
        bt_ctime_left = (TextView) findViewById(R.id.bt_ctime_left);
        bt_ctime_right = (TextView) findViewById(R.id.bt_ctime_right);
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    private int lpvel = 0;//
    private int lpct = 0;//

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initData() {//必须调用
        lpvel = mCourseInfo.getLpvel();//
        lpct = mCourseInfo.getLpct();//

        updateTD();
        updateSN();
    }

    private void updateSN() {
        tv_showtxt.setText(mCourseInfo.getShowmodeStr());
        tv_bxjz.setText(mCourseInfo.getBXJZmodeStr());
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.llSetting_0).setOnClickListener(this);
        findViewById(R.id.llSetting_1).setOnClickListener(this);

        bt_vel_left.setOnClickListener(this);
        bt_vel_right.setOnClickListener(this);
        bt_ctime_left.setOnClickListener(this);
        bt_ctime_right.setOnClickListener(this);

    }

    private int[] CTRL_IDS = {
            R.id.llSetting_0, R.id.llSetting_1,
            R.id.bt_vel_left, R.id.bt_vel_right,
            R.id.bt_ctime_left, R.id.bt_ctime_right};

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    private void updateTD() {

        if (lpvel < 0 || lpvel >= mCourseInfo.VELSET_C.length) {
            lpvel = 0;
        }
        if (lpct < 0 || lpct >= mCourseInfo.CTIMESET_C.length) {
            lpct = 0;
        }

        if (lpvel == 0) {
            bt_vel_left.setEnabled(false);
        } else {
            bt_vel_left.setEnabled(true);
        }

        if (lpvel == mCourseInfo.VELSET_C.length - 1) {
            bt_vel_right.setEnabled(false);
        } else {
            bt_vel_right.setEnabled(true);
        }

        if (lpct == 0) {
            bt_ctime_left.setEnabled(false);
        } else {
            bt_ctime_left.setEnabled(true);
        }

        if (lpct == mCourseInfo.CTIMESET_C.length - 1) {
            bt_ctime_right.setEnabled(false);
        } else {
            bt_ctime_right.setEnabled(true);
        }


        mCourseInfo.setLpct(lpct);
        mCourseInfo.setLpvel(lpvel);

        tv_vel_c.setText(mCourseInfo.getLpvelStr());
        tv_ctime_c.setText(mCourseInfo.getLpctStr());

    }
    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        mCourseInfo.save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llSetting_0:
                toActivity(BottomMenuWindow.createIntent(context, CourseInfo.SHOWMODE_NAMES)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.course_set0)), REQUEST_TO_BOTTOM_MENU_1, false);
                break;
            case R.id.llSetting_1:
                toActivity(BottomMenuWindow.createIntent(context, CourseInfo.BXJZMODE_NAMES)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.course_set1)), REQUEST_TO_BOTTOM_MENU_2, false);
                break;
            case R.id.bt_vel_left:
                lpvel--;
                updateTD();
                break;
            case R.id.bt_vel_right:
                lpvel++;
                updateTD();
                break;
            case R.id.bt_ctime_left:
                lpct--;
                updateTD();
                break;
            case R.id.bt_ctime_right:
                lpct++;
                updateTD();
                break;
            default:
                break;
        }
    }

    private final int REQUEST_TO_BOTTOM_MENU_1 = 31;
    private final int REQUEST_TO_BOTTOM_MENU_2 = 32;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < CourseInfo.SHOWMODE_NAMES.length) {
                        mCourseInfo.setShowmode(selectedPosition);
                        updateSN();
                    }
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_2:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < CourseInfo.BXJZMODE_NAMES.length) {
                        mCourseInfo.setBxjzmode(selectedPosition);
                        updateSN();
                    }
                }
                break;
        }
    }

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //内部类,尽量少用<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //内部类,尽量少用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}
